package com.walker.service.meta;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.walker.util.CommonUtils;
import com.walker.util.JdbcUtil;

@Service
public class ExternalRestServ {
	
	@Autowired
	@Qualifier("dmJdbcTemplate")
	private JdbcTemplate gJdbc;
	
	 public List<Map<String, Object>> getTableInfo(Map<String, Object> params) {
		 String ds_id = String.valueOf(params.get("ds_id"));
		 String url = "",dbType = "" ;
		 Map<String,String> jdbcMap;
		 try {
			 Map<String, Object> data = gJdbc.queryForMap("select ds.id,ds.data_source_type ,param_value from t_meta_data_source ds,t_meta_data_source_param param  "
			 		+ "where ds.id = param.id and param.param_key in ('jdbcurl','http_urls') and ds.id=? limit 1",ds_id);
			 url = data.get("param_value").toString();
			 dbType = data.get("data_source_type").toString();
			 jdbcMap = JdbcUtil.parseJdbc(url, dbType);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
		 
		 url = jdbcMap.get("url");
		 if ("".equals(CommonUtils.valueOf(url))) {
			 return null;
		 }
		 
		 String db_code = String.valueOf(params.get("db_code"));
		 String table_code = String.valueOf(params.get("table_code"));
		 String columns_code = String.valueOf(params.get("columns_code"));
		 String where = " and db_code='" + db_code + "' and table_code='" + table_code + "'";
		 if ("".equals(table_code) || "null".equals(table_code)) {
			 where = " and db_code='" + db_code + "'";
		 }
		 if (("".equals(db_code) || "null".equals(db_code)) && ("".equals(table_code) || "null".equals(table_code))) {
			 where = " and 1=1";
		 }
		 if ("".equals(columns_code) || "null".equals(columns_code)) {
			 columns_code = " * ";
		 }
		 String sql = "select " + columns_code + " from t_asset_meta_tab_collect where jdbc_url=? " + where;
		 List<Map<String, Object>> tableData = gJdbc.queryForList(sql,url);
		 return tableData;
	 }
	 
	 public List<Map<String, Object>> getColumnInfo(Map<String, Object> params) {
//		 String ds_id = String.valueOf(params.get("ds_id"));
//		 String db_code = String.valueOf(params.get("db_code"));
		 String table_id = String.valueOf(params.get("table_id"));
		 String columns_code = String.valueOf(params.get("columns_code"));
		 if ("".equals(columns_code) || "null".equals(columns_code)) {
			 columns_code = " c.* ";
		 } else {
			 String[] colArry = columns_code.trim().split(",");
			 columns_code = "";
			 for (int i=0; i<colArry.length; i++) {
				 columns_code = columns_code + ",c." + colArry[i];
			 }
			 columns_code = columns_code.substring(1);
		 }
		 String sql = "select " + columns_code + " from t_asset_meta_col_collect c,t_asset_meta_tab_collect t where c.table_id = t.table_id and t.table_id=? ";
		 List<Map<String, Object>> colData = gJdbc.queryForList(sql,table_id);
		 return colData;
	 }
	 
	 public List<Map<String, Object>> getPartitionInfo(Map<String, Object> params) {
//		 String ds_id = String.valueOf(params.get("ds_id"));
//		 String db_code = String.valueOf(params.get("db_code"));
		 String table_id = String.valueOf(params.get("table_id"));
		 String columns_code = String.valueOf(params.get("columns_code"));
		 if ("".equals(columns_code) || "null".equals(columns_code)) {
			 columns_code = " p.* ";
		 } else {
			 String[] colArry = columns_code.trim().split(",");
			 columns_code = "";
			 for (int i=0; i<colArry.length; i++) {
				 columns_code = columns_code + ",p." + colArry[i];
			 }
			 columns_code = columns_code.substring(1);
		 }
		 String sql = "select " + columns_code + " from t_asset_meta_prt_collect p,t_asset_meta_tab_collect t where p.table_id = t.table_id and t.table_id=? ";
		 List<Map<String, Object>> colData = gJdbc.queryForList(sql,table_id);
		 return colData;
	 } 
	 
	 public List<Map<String, Object>> getDSList(String project_id,String db_type) {
		 return gJdbc.queryForList("select p.project_id ,ds.id as ds_id,ds.data_source_code,ds.data_source_name,p.param_value as db_code "
		 		+ "from t_meta_project_ds p,t_meta_data_source ds "
		 		+ "left join (select id,param_value from t_meta_data_source_param where param_key='database') p on ds.id = p.id "
		 		+ "where p.ds_id = ds.id and lower(ds.data_source_type) = ? and p.project_id = ? ",db_type.toLowerCase(),project_id);
	 }
	 
	 public List<Map<String, Object>> getDSParam(String ds_id) {
		 return gJdbc.queryForList("select ds.id,ds.data_source_code ,ds.data_source_name,param.param_key,param.param_value "
		 		+ "from t_meta_data_source ds,t_meta_data_source_param param "
		 		+ "where ds.id = param.id and ds.id= ?",ds_id);
	 }
	 
}
